﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cadcam7Moddel.Model;
using Cadcam7Moddel.Context;

namespace camcad7Home.Controllers
{
    public class SubjectController : Controller
    {
        private DataContext db = new DataContext();

        //abc
        // GET: /Subject/
       public ActionResult jsonList()
        {

            string a = Request.Cookies["_culture"]["lang"];
            var list = from o in db.Subjects where o.LangID == a select new {o.ID,o.Name};
            return Json(list, JsonRequestBehavior.AllowGet);
        } 
		
        public ActionResult Index()
        {

            return View();

        }

        //
        // GET: /Subject/Details/5

        public ActionResult Details(int id = 0)
        {
            Subject subject = db.Subjects.Find(id);
            if (subject == null)
            {
                return HttpNotFound();
            }
            return View(subject);
        }

        //
        // GET: /Subject/Create

        public ActionResult Create()
        {
          //  ViewBag.LangID = new SelectList(db.NgonNgus, "Ma", "TenNgonNgu");
            return View();
        }

        //
        // POST: /Subject/Create
		   [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(Subject subject)
        {
            if (ModelState.IsValid)
            {
                subject.NoiDung = HttpUtility.HtmlEncode(subject.NoiDung);
                string a = Request.Cookies["_culture"]["lang"];
                subject.LangID = a;
                db.Subjects.Add(subject);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            //ViewBag.LangID = new SelectList(db.NgonNgus, "Ma", "TenNgonNgu", subject.LangID);
            return View(subject);
        }

        //
        // GET: /Subject/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Subject subject = db.Subjects.Find(id);
            if (subject == null)
            {
                return HttpNotFound();
            }
            subject.NoiDung = HttpUtility.HtmlDecode(subject.NoiDung);
           // ViewBag.LangID = new SelectList(db.NgonNgus, "Ma", "TenNgonNgu", subject.LangID);
            return View(subject);
        }

        //
        // POST: /Subject/Edit/5
		   [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(Subject subject)
        {
            if (ModelState.IsValid)
            {
                subject.NoiDung = HttpUtility.HtmlEncode(subject.NoiDung);
                db.Entry(subject).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            //ViewBag.LangID = new SelectList(db.NgonNgus, "Ma", "TenNgonNgu", subject.LangID);
            return View(subject);
        }

        //
        // GET: /Subject/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Subject subject = db.Subjects.Find(id);
            if (subject == null)
            {
                return HttpNotFound();
            }
            return View(subject);
        }

        //
        // POST: /Subject/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Subject subject = db.Subjects.Find(id);
            db.Subjects.Remove(subject);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
		[HttpPost]
        public ActionResult ajDelete(int id)
        {
             Subject subject = db.Subjects.Find(id);
			 if (subject == null)
            {
                return Json(new { success = false ,msg="Không tìm thấy đối tượng"});
            }

         
            db.Subjects.Remove(subject);
			db.SaveChanges();
            return Json(new { success=true});
        }
		
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}